<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编码" prop="orderNumber" style="margin-right: 50px;margin-bottom: 30px;">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卖家姓名" prop="sellerName" style="margin-right: 50px;margin-bottom: 30px;">
        <el-input
          v-model="queryParams.sellerName"
          placeholder="请输入卖家姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="货物名称" prop="goodsName" style="margin-right: 50px;margin-bottom: 30px;">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入货物名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="订单状态" prop="status" style="margin-right: 30px;margin-bottom: 30px;">
        <el-select v-model="queryParams.status" placeholder="订单状态" clearable>
          <el-option
            v-for="dict in dict.type.lg_goorder_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间" style="margin-right: 30px;margin-bottom: 30px;margin-bottom: 30px;">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['goorder:every:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['goorder:every:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['goorder:every:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['goorder:every:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="everyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="60">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
<!--      <el-table-column label="id" align="center" prop="id" />-->
      <el-table-column label="订单编号" align="center" prop="logisticsOrderNumber" />
      <el-table-column label="卖家姓名" align="center" prop="sellerName" />
      <el-table-column label="卖家联系方式" align="center" prop="sellerContact" />
      <el-table-column label="起始地" align="center" prop="origin" />
      <el-table-column label="目的地" align="center" prop="destination" />
      <el-table-column label="货物名称" align="center" prop="goodsName" />
<!--      <el-table-column label="卖家提供重量" align="center" prop="providedWeight" />-->
      <el-table-column label="卖家提供重量" align="center">
        <template slot-scope="scope">
          {{ scope.row.providedWeight }} kg
        </template>
      </el-table-column>
      <el-table-column label="货物数量" align="center" prop="shuliang" />
      <el-table-column label="货物单位" align="center" prop="goodsUnit" />
      <el-table-column label="司机姓名" align="center" prop="driverName" />
      <el-table-column label="车辆编号" align="center" prop="vehicleNumber" />
      <el-table-column label="实际重量" align="center">
        <template slot-scope="scope">
          {{ scope.row.verifiedWeight }} kg
        </template>
      </el-table-column>
<!--      <el-table-column label="实际重量" align="center" prop="verifiedWeight" />-->
      <el-table-column label="司机核实数量" align="center" prop="volume" />

      <el-table-column label="检查文本" align="center" prop="inspectionText" />

      <el-table-column label="卖家签名" align="center" prop="sellerSignature" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.sellerSignature" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="司机签名" align="center" prop="driverSignature" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.driverSignature" :width="50" :height="50"/>
        </template>
      </el-table-column>

<!--      <el-table-column label="状态" align="center" prop="status" />-->

      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lg_goorder_status" :value="scope.row.status"/>
        </template>
      </el-table-column>



      <el-table-column label="创建时间" width="200" align="center" prop="createTime" />
      <el-table-column label="操作" align="center"  width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['goorder:every:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:every:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="订单编号" prop="logisticsOrderNumber">
              <el-input v-model="form.logisticsOrderNumber" placeholder="请输入订单编号" :disabled="isEdit" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="司机核实重量" prop="verifiedWeight">
              <el-input v-model="form.verifiedWeight" placeholder="请输入核实重量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="司机核实数量" prop="volume">
              <el-input v-model="form.volume" placeholder="请输入核实数量" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检查文本" prop="inspectionText">
              <el-input v-model="form.inspectionText" placeholder="请输入检查文本" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="司机拍照" prop="image">
          <image-upload v-model="form.image"/>
        </el-form-item>
        <el-form-item label="卖家签名" prop="sellerSignature">
          <image-upload v-model="form.sellerSignature"/>
        </el-form-item>
        <el-form-item label="司机签名" prop="driverSignature">
          <image-upload v-model="form.driverSignature"/>
        </el-form-item>
        <el-form-item label="订单状态" prop="status" v-if="isEdit">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.lg_goorder_status" :key="dict.value" :label="dict.value">
              {{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>


      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { listbill, getBillById, updateBill, addBill,delBill } from '@/api/order/goorder'
import store from "@/store";
export default {
  name: "Bill",
  dicts: ['lg_goorder_status','sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      userId:store.state.user.id, //将缓存的用户id赋值给userId
      userName:store.state.user.name, //将缓存的用户id赋值给userId
      loading: true,
      dateRange:[],
      // 选中数组
      ids: [],
      // 初始化价格区间数组，最小值为0，最大值为空
      priceRange: [null, null],

      // 非单个禁用
      single: true,
      selectedOrderNumber: null, // 记录当前订单编号
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      showDriverListDialog: false, // 控制弹窗显示
      showCar: false,
      // 总条数
      total: 0,
      // 岗位表格数据
      everyList: [],
      driverList: [],
      carList : [],
      // //到时候要调接口
      // goodsUnit: [
      //   { id: 1, name: '公斤' },
      //   { id: 2, name: '吨' },
      //   { id: 3, name: '件' },
      //   { id: 4, name: '箱' },
      //   { id: 5, name: '米' }
      // ],
      goodsName: [
        { id: 1, name: '蔬菜' },
        { id: 2, name: '水果' }
      ],
      goodsNameOptions:[],
      //类型
      cktype:[
        { id: 1, name: '常温' },
        { id: 2, name: '冷冻' }
      ],
      warehouseTypes:[],
      // goodsUnitOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isEdit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: null,
        sellerName: null,
        goodsName: null,
        status : null,
        createTime: null,
      },
      // 表单参数
      form: {
          cktype: null, // 绑定的值应为 id
        goodsNameUnits:null
      },
      rules: {
        // orderNumber: [
        //   { required: true, message: "订单编号不能为空", trigger: "blur" }
        // ],
        // sellerName: [
        //   { required: true, message: "卖家姓名不能为空", trigger: "blur" }
        // ],
        // sellerContact: [
        //   { required: true, message: "卖家联系方式不能为空", trigger: "blur" }
        // ],
        // providedWeight: [
        //   { required: true, message: "卖家提供重量不能为空", trigger: "blur" }
        // ],
        // goodsQuantity: [
        //   { required: true, message: "货物数量不能为空", trigger: "blur" }
        // ],
        // goodsUnit: [
        //   { required: true, message: "货物单位不能为空", trigger: "change" }
        // ],
        // storageDays: [
        //   { required: true, message: "预计存储天数不能为空", trigger: "blur" }
        // ],
        // origin: [
        //   { required: true, message: "起始地不能为空", trigger: "blur" }
        // ],
        // destination: [
        //   { required: true, message: "目的地不能为空", trigger: "blur" }
        // ],
        // verifiedWeight: [
        //   { required: true, message: "核实重量不能为空", trigger: "blur" }
        // ],
        // volume: [
        //   { required: true, message: "核实数量不能为空", trigger: "blur" }
        // ],
        // inspectionText: [
        //   { required: true, message: "检查文本不能为空", trigger: "change" }
        // ],
        // sellerSignature: [
        //   { required: true, message: "卖家签字不能为空", trigger: "blur" }
        // ],
        // driverSignature: [
        //   { required: true, message: "司机签字不能为空", trigger: "blur" }
        // ],
        // status: [
        //   { required: true, message: "订单状态不能为空", trigger: "blur" }
        // ],
        // finalFreightPrice: [
        //   { required: true, message: "最终运费不能为空", trigger: "blur" }
        // ],
        // depositRequired: [
        //   { required: true, message: "需要支付定金不能为空", trigger: "blur" }
        // ],
        // paymentAmount: [
        //   { required: true, message: "余额支付金额不能为空", trigger: "blur" }
        // ],
        // depositPaid: [
        //   { required: true, message: "是否支付定金不能为空", trigger: "change" }
        // ]
      }

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listbill(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.everyList = response.rows;
          this.total = response.total;
          this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = Object.assign({}, {
        orderNumber: null,
        sellerName: null,
        goodsName: null,
        cktype: null,
        priceRange: [null, null]
      });
      // 重新赋值 priceRange，确保 Vue 触发视图更新
      this.resetForm("form");
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {

      this.goodsNameOptions = this.goodsName;
      // this.goodsUnitOptions = this.goodsUnit;
      this.warehouseTypes=this.cktype;
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isEdit = true;
      const billId = row.id || this.ids
      getBillById(billId).then(response => {
        this.form = response.data;
        this.form.status= String(response.data.status);
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
              this.form.createBy=this.userId,
              this.form.updateBy=this.userName,
            updateBill(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBill(this.form).then(response => {
                this.form.createBy=this.userId,
                this.form.updateBy=this.userName,
                this.form.cktype = parseInt(this.form.cktype);
              this.form.goodsName = parseInt(this.form.goodsName);
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const billIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + billIds + '"的数据项？').then(function() {
        return delBill(billIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('content/bill/export', {
        ...this.queryParams
      }, `bill_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
